package services

import (
	"errors"
	"fmt"
	"github.com/silenceper/wechat/message"
	"strconv"
	"strings"
	"weiboxs.com/new_h_glasses/models"
)

//处理微信消息
type WechatMessageService struct {
	message message.MixMessage
	config  map[string]string
	Depid int64
}

//文本消息
func (s *WechatMessageService) MsgTypeText() *message.Reply {
	reply, _ := s.article(s.Depid, s.message.Content)
	return reply
}

//图片消息
func (s *WechatMessageService) MsgTypeImage() *message.Reply { return nil }

//语音消息
func (s *WechatMessageService) MsgTypeVoice() *message.Reply { return nil }

//视频消息
func (s *WechatMessageService) MsgTypeVideo() *message.Reply { return nil }

//小视频消息
func (s *WechatMessageService) MsgTypeShortVideo() *message.Reply { return nil }

//地理位置消息
func (s *WechatMessageService) MsgTypeLocation() *message.Reply { return nil }

//链接消息
func (s *WechatMessageService) MsgTypeLink() *message.Reply { return nil }

//EventSubscribe 订阅
func (s *WechatMessageService) EventSubscribe() *message.Reply {
	//查询图文库有没有订阅图文，如没有则默认读取配置信息数据
	if value, err := s.article(s.Depid,"subscribe"); err == nil { //存在图文消息
		return value
	}
	if _, ok := s.config["EventSubscribe"]; ok {
		return s.text(s.config["EventSubscribe"])
	}
	return s.text("Oops！找不到需要的信息。")
}

//取消订阅
func (s *WechatMessageService) EventUnsubscribe() *message.Reply { return nil }

//用户已经关注公众号，则微信会将带场景值扫描事件推送给开发者
func (s *WechatMessageService) EventScan() *message.Reply { return nil }

//上报地理位置事件
func (s *WechatMessageService) EventLocation() *message.Reply { return nil }

//点击菜单拉取消息时的事件推送
func (s *WechatMessageService) EventClick() *message.Reply {
	reply, _ := s.article(s.Depid,s.message.EventKey)
	return reply
}

//点击菜单跳转链接时的事件推送
func (s *WechatMessageService) EventView() *message.Reply { return nil }

//扫码推事件的事件推送
func (s *WechatMessageService) EventScancodePush() *message.Reply { return nil }

//扫码推事件且弹出“消息接收中”提示框的事件推送
func (s *WechatMessageService) EventScancodeWaitmsg() *message.Reply { return nil }

//弹出系统拍照发图的事件推送
func (s *WechatMessageService) EventPicSysphoto() *message.Reply { return nil }

//弹出拍照或者相册发图的事件推送
func (s *WechatMessageService) EventPicPhotoOrAlbum() *message.Reply { return nil }

//弹出微信相册发图器的事件推送
func (s *WechatMessageService) EventPicWeixin() *message.Reply { return nil }

//弹出地理位置选择器的事件推送
func (s *WechatMessageService) EventLocationSelect() *message.Reply { return nil }

//回复文本消息
func (s *WechatMessageService) text(value string) *message.Reply {
	text := message.NewText(value)
	return &message.Reply{message.MsgTypeText, text}
}

//回复图文消息
func (s *WechatMessageService) article(depid int64, command string) (*message.Reply, error) {
	messageModel := models.WechatMessage{}
	data := messageModel.OneByKeyWord(command)
	if data.Id <= 0 {
		return nil, errors.New("找不到需要的信息")
	}

	articles := make([]*message.Article, 1)
	articles[0] = s.setArticle(data)
	if ars := s.handleRelationArticle(depid, data.RelationIds, articles); ars != nil {
		articles = ars
	}

	return &message.Reply{message.MsgTypeNews, message.NewNews(articles)}, nil
}

func (s *WechatMessageService) setArticle(data *models.WechatMessage) *message.Article {
	article := new(message.Article)
	article.Title = data.Title
	article.Description = data.Description
	article.PicURL = data.Image
	if data.Typeid == 1 {
		article.URL = data.Url
	} else {
		article.URL = strings.Replace(data.Url, "{openid}", fmt.Sprintf("%s", s.message.FromUserName), -1)
	}
	return article
}

//处理图文消息
func (s *WechatMessageService) handleRelationArticle(depid int64, relation_ids string, articles []*message.Article) []*message.Article {

	if len(relation_ids) <= 0 {
		return nil
	}

	ids := strings.Split(relation_ids, "$")
	if len(ids) <= 0 {
		return nil
	}
	messageModel := models.WechatMessage{}

	for k, _ := range ids {
		if k >= 7 {
			break
		}
		if len(ids[k]) <= 0 {
			continue
		}
		id, _ := strconv.ParseInt(ids[k], 10, 64)
		if id == 0 {
			continue
		}
		data := messageModel.One(id, depid)
		if data.Id == 0 {
			continue
		}
		articles = append(articles, s.setArticle(data))
	}
	return articles
}